@isTest
private class test_ProcessRecords {
    
    public static List<User> usersList = new List<User>();
    public static List<Group> groupsList = new List<Group>();
    public static List<QueueSobject> qsList = new List<QueueSobject>();
    public static List<Case> CasesList = new List<Case>();
    public static List<Lead> leadsList = new List<Lead>();
    public static List<GroupMember> gmlist = new List<GroupMember>();
    public static List<QueueSetting__c> qsettingList = new List<QueueSetting__c>();
    public static List<QueuePrioritizationRule__c> qprList = new List<QueuePrioritizationRule__c>();
    
    public static void createTestData() {
        usersList = [select id from User limit 5];
        
        Group rrGroup = new Group();
        rrGroup.Name = 'Group 1';
        rrGroup.Type = 'Queue';
        groupsList.add(rrGroup);
        rrGroup = new Group();
        rrGroup.Name = 'Group 2';
        rrGroup.Type = 'Queue';
        groupsList.add(rrGroup);
        upsert groupsList;
        
        GroupMember gm = new GroupMember();
        gm.GroupId = groupsList[0].Id;
        gm.UserOrGroupId = usersList[0].Id;
        gmList.add(gm);
        
        gm = new GroupMember();
        gm.GroupId = groupsList[1].Id;
        gm.UserOrGroupId = usersList[1].Id;
        gmList.add(gm);
        
        gm = new GroupMember();
        gm.GroupId = groupsList[1].Id;
        gm.UserOrGroupId = usersList[2].Id;
        gmList.add(gm);
        
        gm = new GroupMember();
        gm.GroupId = groupsList[1].Id;
        gm.UserOrGroupId = usersList[3].Id;
        gmList.add(gm);
        
        QueueSobject qs = new QueueSobject();
        qs.SobjectType = 'Case';
        qs.QueueId = groupsList[0].Id;
        qsList.add(qs);
        qs = new QueueSobject();
        qs.SobjectType = 'Lead';
        qs.QueueId = groupsList[1].Id;
        qsList.add(qs);
        System.runAs(new User(Id = UserInfo.getUserId(),Working_days__c = 'Sunday;Monday;Tuesday;Wednesday;Thurthday;Friday;Saturday'))
        {upsert qsList;}
    }
    
    public static void createTestData2() {   
        
        QueueSetting__c qsetting;
        for (Integer i=0; i<20; i++) {
            qsetting = new QueueSetting__c();
            qsetting.Active__c = true;
            qsetting.QueueId__c = qsList[0].QueueId;
            qsetting.AssignmentType__c = 'Load Balanced';
            qsettingList.add(qsetting);
        }
        
        qsetting = new QueueSetting__c();
        qsetting.Active__c = true;
        qsetting.QueueId__c = qsList[1].QueueId;
        qsetting.AssignmentType__c = 'Round Robin';
        qsettingList.add(qsetting);
        
        upsert qsettingList;
        
        QueuePrioritizationRule__c qpr1 = new QueuePrioritizationRule__c();
        qpr1.QueueSettingId__c = qsettingList[0].Id;
        qpr1.FieldAPIName__c = 'Status';
        qpr1.ExecutionOrder__c = 2;
        qprList.add(qpr1);
        
        QueuePrioritizationRule__c qpr2 = new QueuePrioritizationRule__c();
        qpr2.QueueSettingId__c = qsettingList[1].Id;
        qpr2.FieldAPIName__c = 'Type';
        qpr2.ExecutionOrder__c = 1;
        qprList.add(qpr2);
        
        upsert qprList;
        
        Case c = new Case();
        c.OwnerId = qsList[0].QueueId;
        c.Status = 'New';
        c.Origin = 'Phone';
        CasesList.add(c);
        upsert CasesList;
        
        Lead l = new Lead();
        l.OwnerId = qsList[1].QueueId;
        l.LastName = 'DS';
        l.Company = 'DS';
        leadsList.add(l);
        upsert leadsList;
        
    }
    
    public static testMethod void processTest() {
        createTestData();
        createTestData2();
        SchedulerClass sched = new SchedulerClass();
        sched.isTest = true;
        sched.execute(null);
        ProcessRecords cntrl2 = new ProcessRecords(CasesList, true, true);
        cntrl2.processObjects(CasesList, true);
        
        ManualControl_controller manualController = new ManualControl_controller();
        manualController.runScheduler();
        
        ProcessRecords prController = new ProcessRecords(null,null,null);
        prController.start(null);
        prController.execute(null,null);
        prController.finish(null);
    }
    
}